﻿using Engine;
using Engine.Graphics;
using Game;
namespace Mekiasm
{
    public class EmptyFluidCell : ItemFlat,Interface.IChargeAbleItem
    {
        public static int Index;
        private Texture2D Empty = ContentManager.Get<Texture2D>("Mekiasm/Textures/Cell/empty_cell");
        private Texture2D NotEmpty = ContentManager.Get<Texture2D>("Mekiasm/Textures/Cell/water_cell");

        public EmptyFluidCell() : base(string.Empty, null)
        {
        }

        public override string GetDisplayName(SubsystemTerrain subsystemTerrain, int value)
        {
            long d = GetCurrentAmount(value);
            if (d == 0) return "空的流体单元";
            else
            {
                int v = GetChargeFuelValue(value);
                string n = BlocksManager.Blocks[Terrain.ExtractContents(v)].GetDisplayName(null, v);
                return $"流体单元-{n}";
            }
        }

        public ComponentEnergyMachine.FuelType ChargeFuelType => ComponentEnergyMachine.FuelType.Fliud;

        public long MaxAmount => GetMaxPower();

        public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {
            long d = GetCurrentAmount(value);
            BlocksManager.DrawFlatBlock(primitivesRenderer, value, size, ref matrix, d == 0 ? Empty : NotEmpty, color, false, environmentData);
        }
        public override bool OnUse(Ray3 aim, ComponentMiner componentMiner, TerrainRaycastResult? raycast)
        {
            if (raycast.HasValue)
            {
                if (GetCurrentAmount(componentMiner.ActiveBlockValue) >= GetMaxPower())
                {
                    //倒出
                    var p = raycast.Value.CellFace.Point;
                    if (ILibrary.SubsystemTerrain.Terrain.GetCellContents(p.X, p.Y + 1, p.Z) == 0)
                    {
                        componentMiner.m_subsystemTerrain.ChangeCell(p.X, p.Y + 1, p.Z, GetChargeFuelValue(componentMiner.ActiveBlockValue));
                        SaveItemSystem.ChangePower(componentMiner.Inventory, componentMiner.Inventory.ActiveSlotIndex, -GetMaxPower(), 0);
                    }
                }
                else
                {
                    //装入
                    object obj = componentMiner.Raycast(aim, RaycastMode.Gathering);
                    if (obj is TerrainRaycastResult result && Terrain.ExtractContents(result.Value) == WaterBlock.Index && FluidBlock.GetLevel(Terrain.ExtractData(result.Value)) == 0)
                    {
                        if (GetCurrentAmount(componentMiner.ActiveBlockValue) + GetMaxPower() <= MaxAmount)
                        {
                            var p = result.CellFace.Point;
                            SaveItemSystem.ChangePower(componentMiner.Inventory, componentMiner.Inventory.ActiveSlotIndex, GetMaxPower(), WaterBlock.Index);
                            componentMiner.m_subsystemTerrain.ChangeCell(p.X, p.Y, p.Z, 0);
                        }
                    }
                }
            }
            return false;
        }
        public override float GetBlockHealth(int value)
        {
            SaveItemSystem.GetBlockData(value, out var d);
            if (d == null) return -1f;
            else return GetCurrentAmount(value) / (float)GetMaxPower();
        }

        public override int GetMaxPower()
        {
            return 1024;
        }

        public bool CanAcceptFuel(int value)
        {
            return true;
        }

        public long GetCurrentAmount(int value)
        {
            SaveItemSystem.GetBlockData(value, out var d);
            if (d == null) return 0;
            else return d.fuelAmount;
        }

        public int SetCurrentAmount(int value, int fuelValue, long varAmount)
        {
            return SaveItemSystem.ChangePower(value, varAmount, fuelValue);
        }

        public int GetChargeFuelValue(int value)
        {
            SaveItemSystem.GetBlockData(value, out var d);
            if (d == null)
            {
                return 0;
            }
            else return d.fuelValue;
        }

    }
}
